package memberrewards.delegate;

import java.util.ArrayList;
import java.util.List;

import memberrewards.dao.BonusOfferDAO;
import memberrewards.dao.CatalogServiceDAO;
import memberrewards.dao.ServiceDAO;
import memberrewards.dtos.AccountAdditionalInfoDTO;
import memberrewards.dtos.AccountDetailDTO;
import memberrewards.dtos.BonusOfferDTO;
import memberrewards.dtos.CatalogItemDTO;
import memberrewards.dtos.TierConfigDTO;
import memberrewards.entities.AccountAdditionalInfo;
import memberrewards.entities.AccountDetail;
import memberrewards.entities.BonusOffer;
import memberrewards.entities.CatalogItem;
import memberrewards.entities.TierConfig;
import static memberrewards.helpers.Constants.BLANK;

/**
 * 
 * Decouple PT/WT from BT
 *
 */

public class RewardsDelegate {
	
	private RewardsDelegate(){
		super();
	}
	
	public static AccountDetailDTO getAccountDetail(final String pan) {
		if(pan == null || BLANK.equals(pan)){
			return null;//better throw a NullPointerException
		}
		AccountDetailDTO accountDetailDTO=null;
		AccountDetail accountDetail=ServiceDAO.getAccountDetail(pan);
		if(accountDetail != null){
			 accountDetailDTO= new AccountDetailDTO();
			 accountDetailDTO=copyProperties(accountDetailDTO,accountDetail);
			 System.out.println("RewardsDelegate :: getAccountDetail:: Value:-"+accountDetailDTO);
		}else{
			System.out.println("Got Null Value in getAccountDetail PAN ->['"+pan+"']");
		}
		return accountDetailDTO;
	}
	
	public static CatalogItemDTO getCatalogItem(final String itemid) {
		if(itemid==null || BLANK.equals(itemid)){
			return null;
		}
		CatalogItemDTO catalogItemDTO=null;
		CatalogItem catalogItem=CatalogServiceDAO.getCatalogItem(itemid);
		if(catalogItem!=null){
			catalogItemDTO= new CatalogItemDTO();
			catalogItemDTO=copyProperties(catalogItemDTO,catalogItem);
			System.out.println("RewardsDelegate :: getCatalogItem:: Value:-"+catalogItemDTO);
		}
		else{
			System.out.println("Got Null Value in getCatalogItem ITEMID ->['"+itemid+"']");
		}
		return catalogItemDTO;
	}
	
	public static ArrayList<CatalogItemDTO> getEligibleCatalogList(final String tier) {
		final ArrayList<CatalogItem> eligibleCatalogList =CatalogServiceDAO.getEligibleCatalogList(tier);
		ArrayList<CatalogItemDTO> catalogItemDTOList=new ArrayList<CatalogItemDTO>();
		for (final CatalogItem catalogItem : eligibleCatalogList) {
			CatalogItemDTO dto= new CatalogItemDTO();
			dto=(CatalogItemDTO) copyProperties(dto, catalogItem);
			if(dto!=null){
				System.out.println("RewardsDelegate :: getEligibleCatalogList:: Value:-"+dto);
				catalogItemDTOList.add(dto);
			}else{
				System.out.println("Got Null Value in getEligibleCatalogList TIER ->['"+tier+"']");
			}
		}
		return catalogItemDTOList;
	}
	
	public static ArrayList<CatalogItemDTO> getSearchResult(final String tier,final  String searchKey ) {
		final ArrayList<CatalogItem> eligibleCatalogList =CatalogServiceDAO.getSearchResult(tier, searchKey);
		ArrayList<CatalogItemDTO> catalogItemDTOList=new ArrayList<CatalogItemDTO>();
		for (final CatalogItem catalogItem : eligibleCatalogList) {
			CatalogItemDTO dto= new CatalogItemDTO();
			dto=(CatalogItemDTO) copyProperties(dto, catalogItem);
			if(dto != null){
				System.out.println("RewardsDelegate :: getSearchResult:: Value:-"+dto);
				catalogItemDTOList.add(dto);
			}else{
				System.out.println("Got Null Value in getSearchResult TIER ->['"+tier+"'] SEARCHKEY ->['"+searchKey+"']");
			}
		}
		return catalogItemDTOList;
	}
	
	
	public static ArrayList<BonusOfferDTO> getEligibleBonusOffers(final String tier) {
		final ArrayList<BonusOffer> eligibleBonusOfferList = BonusOfferDAO.getEligibleBonusOffers(tier);
		ArrayList<BonusOfferDTO> bonusOfferDTOList=new ArrayList<BonusOfferDTO>();
		for (final BonusOffer bonusOffer : eligibleBonusOfferList) {
			BonusOfferDTO dto= new BonusOfferDTO();
			dto=(BonusOfferDTO) copyProperties(dto, bonusOffer);
			if(dto!=null){
				System.out.println("RewardsDelegate :: getEligibleBonusOffers:: Value:-"+dto);
				bonusOfferDTOList.add(dto);
			}else{
				System.out.println("Got Null Value in getEligibleBonusOffers TIER ->['"+tier+"']");
			}
		}
		return bonusOfferDTOList;
	}
	
	public static TierConfigDTO getTierConfig(final String tier) {
		if(tier==null  || BLANK.equals(tier)){
			return null;//better throw a NullPointerException
		}
		TierConfigDTO tierConfigDTO=null;
		TierConfig tierConfig=ServiceDAO.getTierDetail(tier);
		if(tierConfig!=null){
			tierConfigDTO= new TierConfigDTO();
			tierConfigDTO=copyProperties(tierConfigDTO,tierConfig);
			 System.out.println("RewardsDelegate :: getTierConfig:: Value:-"+tierConfigDTO);
		}else{
			System.out.println("Got Null Value in getTierConfig TIER ->['"+tier+"']");
		}
		return tierConfigDTO;
	}
	
	public static boolean enrollAccount(AccountDetailDTO accountDetail){
		AccountDetail tmpAccountDetail= new AccountDetail();
		copyProperties(tmpAccountDetail,accountDetail);
		return ServiceDAO.enrollAccount(tmpAccountDetail);
	}
	
	public static boolean updateAccountDetail(AccountDetailDTO accountDetail){
		AccountDetail tmpAccountDetail= new AccountDetail();
		copyProperties(tmpAccountDetail,accountDetail);
		return ServiceDAO.updateAccountDetail(tmpAccountDetail);
		
	}
	
	public static boolean addAccountAdditionalInfo(AccountAdditionalInfoDTO accountAdditionalInfo){
		AccountAdditionalInfo tmpAccountAdditionalInfo= new AccountAdditionalInfo();
		copyProperties(tmpAccountAdditionalInfo,accountAdditionalInfo);
		return ServiceDAO.addAccountAdditionalInfo(tmpAccountAdditionalInfo);
	}
	
	public static ArrayList<AccountAdditionalInfoDTO> getAccountAdditionalInfos(final String pan) {
		if(pan==null || BLANK.equals(pan)){
			return null;
		}
		 final List<AccountAdditionalInfo> additionalInfos=ServiceDAO.getAccountAdditionalInfos(pan);
		 ArrayList<AccountAdditionalInfoDTO> additionalInfoDTOs=new ArrayList<AccountAdditionalInfoDTO>(); 
		 if(additionalInfos!=null){
			 for (final AccountAdditionalInfo accountAdditionalInfo : additionalInfos) {
				 AccountAdditionalInfoDTO dto = new AccountAdditionalInfoDTO();
				 dto=copyProperties(dto,accountAdditionalInfo);
				 if(dto!=null){
					 additionalInfoDTOs.add(dto);
					 System.out.println("RewardsDelegate :: getAccountAdditionalInfos:: Value:-"+dto);
				 }
				 else{
					 System.out.println("Got Null Value for AccountAdditionalInfoDTO");
				 }
			}
		 }
		 else{
			 System.out.println("Got Null Value for getAccountAdditionalInfos PAN ->['"+pan+"']");
		 }
		 return additionalInfoDTOs;
	}
	
	/**
	 * XXX-Better delegate  this to org.apache.commons.beanutils.BeanUtils;
	 * @param dest
	 * @param source
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private static final <D,S> D   copyProperties(final D dest,final S source){
		if(dest instanceof AccountDetailDTO){
			return (D) copyAccountDetailPropertiesToAccountDetailDTO((AccountDetailDTO)dest,(AccountDetail)source);
		}
		else if(dest instanceof BonusOfferDTO){
			return (D)copyBonusOfferPropertiesToBonusOfferDTO((BonusOfferDTO)dest,(BonusOffer)source);
		}
		else if(dest instanceof CatalogItemDTO){
			return (D)copyCatalogItemPropertiesToCatalogItemDTO((CatalogItemDTO)dest,(CatalogItem)source);
		}
		else if(dest instanceof AccountDetail){
			return (D) copyAccountDetailDtoPropertiesToAccountDetail((AccountDetail)dest,(AccountDetailDTO)source);
		}
		else if(dest instanceof TierConfigDTO){
			return (D)copyTierConfigPropertiesToTierConfigDTO((TierConfigDTO)dest,(TierConfig)source);
		}
		else if(dest instanceof AccountAdditionalInfo){
			return (D)copyAccountAdditionalInfoDtoPropertiesToAccountAdditionalInfo((AccountAdditionalInfo)dest,(AccountAdditionalInfoDTO)source);
		}
		else if(dest instanceof AccountAdditionalInfoDTO){
			return (D)copyAccountAdditionalInfoPropertiesToAccountAdditionalInfoDTO((AccountAdditionalInfoDTO)dest,(AccountAdditionalInfo)source);
		}
		return dest;
	}
	
	private static final AccountDetailDTO copyAccountDetailPropertiesToAccountDetailDTO(AccountDetailDTO des,AccountDetail src){
		if( des!=null && src!=null ){
			des.setPan(src.getPan());
			des.setPointBalance(src.getPointBalance());
			des.setTier(src.getTier());
			TierConfig tierConfig=src.getTierConfig();
			if(tierConfig!=null){
				TierConfigDTO tmpTierConfigDTO= new TierConfigDTO();
				tmpTierConfigDTO.setCostPerPoint(tierConfig.getCostPerPoint());
				tmpTierConfigDTO.setTier(tierConfig.getTier());
				des.setTierConfig(tmpTierConfigDTO);
			}
		}
		return des;
	}
	
	private static final BonusOfferDTO copyBonusOfferPropertiesToBonusOfferDTO(BonusOfferDTO des,BonusOffer src){
		if( des!=null && src!=null ){
			des.setDescription(src.getDescription());
			des.setEligibleMerchants(src.getEligibleMerchants());
			des.setEligibleTiers(src.getEligibleMerchants());
			des.setOfferId(src.getOfferId());
			des.setOfferType(src.getOfferType());
			des.setTierList(src.getTierList());
		}
		return des;
	}
	
	private static final CatalogItemDTO copyCatalogItemPropertiesToCatalogItemDTO(CatalogItemDTO des,CatalogItem src){
		if(des!=null && src!=null){
			des.setPointCost(src.getPointCost());
			des.setDescription(src.getDescription());
			des.setEligibleTiers(src.getEligibleTiers());
			des.setItemid(src.getItemid());
			des.setMake(src.getMake());
			des.setName(src.getName());
			des.setTierList(src.getTierList());
		}
		return des;
	}
	
	private static final TierConfigDTO copyTierConfigPropertiesToTierConfigDTO(TierConfigDTO des,TierConfig src){
		if( des!=null && src!=null ){
			des.setTier(src.getTier());
			des.setCostPerPoint(src.getCostPerPoint());
		}
		return des;
	}
	
	private static final AccountAdditionalInfo copyAccountAdditionalInfoDtoPropertiesToAccountAdditionalInfo(AccountAdditionalInfo des,AccountAdditionalInfoDTO src){
		if( des!=null && src!=null ){
			des.setPan(src.getPan());
			des.setAdditionalInfoId(src.getAdditionalInfoId());
			des.setAdditionalInfoDescription(src.getAdditionalInfoDescription());
		}
		return des;
	}
	
	private static final AccountAdditionalInfoDTO copyAccountAdditionalInfoPropertiesToAccountAdditionalInfoDTO(AccountAdditionalInfoDTO des,AccountAdditionalInfo src){
		if( des!=null && src!=null ){
			des.setPan(src.getPan());
			des.setAdditionalInfoId(src.getAdditionalInfoId());
			des.setAdditionalInfoDescription(src.getAdditionalInfoDescription());
		}
		return des;
	}
	
	private static final AccountDetail copyAccountDetailDtoPropertiesToAccountDetail(AccountDetail des,AccountDetailDTO src){
		if( des!=null && src!=null ){
			des.setPan(src.getPan());
			des.setPointBalance(src.getPointBalance());
			des.setTier(src.getTier());
		}
		return des;
	}
}
